/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package foxmob.mlts.model;

import foxmob.mlts.component.FileManager;
import foxmob.mlts.model.Question.QuestionType;
import java.util.ArrayList;
import java.util.UUID;

/**
 *
 * @author Emmanouil Samatas
 */
public class Section {
    private String title;
    private String description;
    private String ID;
    private int remainingSeconds;
    private ArrayList<Subsection> subsections;
    
    public Section(){
        subsections = new ArrayList<Subsection>();
        UUID sectionID = UUID.randomUUID();
        ID = sectionID.toString();
    }
    
    @Override
    public String toString() {
        return title;
    }
    
    public String getID() {
        return ID;
    }
    
    public void setID(String id) {
        this.ID = id;
    }
    
    public void addSubsection(Subsection subsection) {
        // add to subsection array
        // setsubsection ID by adding 1 of the current subsection length
       
        this.subsections.add(subsection);
    }
    
    public void addSubsection(ArrayList<Subsection> subsection) {
        // loop through a list of subsection
        for (Subsection sub : subsection) {
            addSubsection(sub);
        }
    }
    
    public void removeSubSection(Subsection subsection) {
        this.subsections.remove(subsection);
    }
    
    public void removeSubSection(ArrayList<Subsection> subsection) {
        // loop through a list of section
        for (Subsection sub : subsection) {
            removeSubSection(sub);
        }
    }
    
    public boolean isExistSubsection(Subsection subsection) {
        for (Subsection sub : this.subsections) {
            if (sub.getTitle().equals(subsection.getTitle())) {
                System.out.print(subsection.getTitle());
                return true;
            }
        }
        return false;
    }
    
    public void modifySubSection(Subsection subsection) {
        int i = 0;
        for (Subsection q : subsections) {
            if (q.getID().equals(subsection.getID())) {
                 break;
            }
            ++i;
        } 
        this.subsections.set(i, subsection);
    }
    
    public int getSubsectionLength() {
        return subsections.size();
    }
    
    public Subsection getSubsectionWithID(String ID) {
        for (Subsection sub : subsections) {
            if (sub.getID().equals(ID)) {
                return sub; 
            }
        }
        return null;
    }
    
    public void setTitle(String title) {
        this.title = title;
    }
    
    public String getTitle() {
        return title;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    public String getDescription() {
        return description;
    }
    
    public void setRemainingSeconds(int remainingSeconds) {
        this.remainingSeconds = remainingSeconds;
    }
    
    public int getRemainingSeconds() {
        return remainingSeconds;
    }
    
    public void setSubsections(ArrayList<Subsection> subsection) {
        this.subsections = subsection;
    }
    
    public ArrayList<Subsection> getSubsections() {
        return subsections;
    }
    
    public ArrayList<Question> getQuestions(QuestionType qestionType) {
        ArrayList<Question> questions = new ArrayList<Question>();
        Class questionClass = Question.classFromQuestionType(qestionType);
        for (Question question : getQuestions()) {
            if (question.getClass().equals(questionClass)) {
                questions.add(question);
            }
        }
        return questions;
    }
    
    public ArrayList<Question> getQuestions() {
        
        ArrayList<Question> questions= new ArrayList();
        for(Subsection sub : this.getSubsections())
        {
            questions.addAll(sub.getQuestions());
        }
        return questions;
    }
}
